/// Created by Nazariy Jaworski Copyright (c) 2013
/// Bot program
///
/// botui.cpp -
///  implements user interface (console for now) for
///  controlling application behavior
///
/// To Do list:
///  - ...
///
/// v 0.1        03.04.2013

#include "botui.h"
#include "bot.h"

BotUI::BotUI() ///< Common constructor
{
    isOk = true;
    _myWServerRunner = new WebServerRunner();   /// \todo perhaps it should be in stack?
    // keep it clean, use 'run()' for extra initializations
}

void BotUI::run()
{
    std::cout << UI_STR_TITLE_LINE << UI_STR_MESSAGE_INFO;

    // WARNING! Endless loop - use command 'exit' to terminate it
    for(;;)
    {
        std::cout << UI_STR_TITLE_LINE;
        QTextStream qtin(stdin);
        qtin >> lastCommand;
        //Q_EMIT SIGNAL_WriteWebUIString(lastCommand);
        _hiddenLogText += "[console]" + lastCommand + "\n";
        BotWtUI::updateAllUserSessions();

        if(!lastCommand.compare(UI_STR_COMMAND_EXIT))
            break;
        else if(!lastCommand.compare(UI_STR_COMMAND_INFO))
            std::cout << UI_STR_TITLE_LINE << UI_STR_MESSAGE_INFO;
        else if(!lastCommand.compare(UI_STR_COMMAND_REGISTER))
            Q_EMIT SIGNAL_RegisterAtEnvironment();
        else if(!lastCommand.compare(UI_STR_COMMAND_CLIENTS))
            Q_EMIT SIGNAL_ShowClients();
        else if(!lastCommand.compare(UI_STR_COMMAND_OCLPLATFORMS))
            Q_EMIT SIGNAL_ShowOCLPlatforms();
        else if(!lastCommand.compare(UI_STR_COMMAND_OCLDEVICES))
            Q_EMIT SIGNAL_ShowOCLDevices();
        else if(!lastCommand.compare(UI_STR_COMMAND_RUNWEBSERVER))
            //Q_EMIT SIGNAL_RunWebSerwer();
        {
            if(_myWServerRunner)
            {
                if(!_myWServerRunner->isRunning() /*&& Bot::GetInstance()->myCommunicator->myServer */)
                {
                    _myWServerRunner->start();
                }
                else if(_myWServerRunner->isRunning())
                    SLOT_WriteUIString(" Error!: Web Server is already running\n");
                //else if(!Bot::GetInstance()->myCommunicator->myServer)
                //    SLOT_WriteUIString(" Error!: Bot is not in the Server mode\n");
            }
            else Q_EMIT SIGNAL_FatalError(" FATAL ERROR: UI web server runner was not constructed\n");
        }
        else if(!lastCommand.compare(UI_STR_COMMAND_STOPWEBSERVER))
            Q_EMIT SIGNAL_StopWebSerwer();  /// \todo
    }
    Q_EMIT SIGNAL_Exit();
}

BotUI::~BotUI() // Common destructor
{
    if(_myWServerRunner)
        delete _myWServerRunner;
}
